package com.example.mobifin_busticket.models;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.mobifin_busticket.AppData;

import android.R.bool;

public class CancellationTicketDetailsModel {

	String key, value, fareKey, fareValue, freeCancellationTime, tatkalTime,strJSOn;
	boolean cancellable, partiallyCancellable;
	ArrayList<CancellationTicketDetailsModel> CancelTicketList = new ArrayList<CancellationTicketDetailsModel>();
	ArrayList<CancellationTicketDetailsModel> fareList = new ArrayList<CancellationTicketDetailsModel>();
	String strTicketNo;
	ArrayList<String> seatDetails = new ArrayList<String>();

	public ArrayList<CancellationTicketDetailsModel> getfareList() {
		return fareList;
	}

	private void setfareList(ArrayList<CancellationTicketDetailsModel> fareList) {
		// TODO Auto-generated method stub
		this.fareList = fareList;
	}

	public ArrayList<CancellationTicketDetailsModel> getCancelTicketList() {
		return CancelTicketList;
	}

	public void setCancelTicketList(
			ArrayList<CancellationTicketDetailsModel> CancelTicketList) {
		this.CancelTicketList = CancelTicketList;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getvalue() {
		return value;
	}

	public void setvalue(String value) {
		this.value = value;
	}

	public String getfareKey() {
		return fareKey;
	}

	public void setfareKey(String fareKey) {
		this.fareKey = fareKey;
	}

	public String getfareValue() {
		return fareValue;
	}

	public void setfareValue(String fareValue) {
		this.fareValue = fareValue;
	}

	public String getfreeCancellationTime() {
		return freeCancellationTime;
	}

	public void setfreeCancellationTime(String freeCancellationTime) {
		this.freeCancellationTime = freeCancellationTime;
	}

	public String gettatkalTime() {
		return tatkalTime;
	}

	public void settatkalTime(String tatkalTime) {
		this.tatkalTime = tatkalTime;
	}

	public boolean getcancellable() {
		return cancellable;
	}

	public void setcancellable(boolean cancellable) {
		this.cancellable = cancellable;
	}

	public boolean getpartiallyCancellable() {
		return partiallyCancellable;
	}

	public void setpartiallyCancellable(boolean partiallyCancellable) {
		this.partiallyCancellable = partiallyCancellable;
	}

	public static ArrayList<CancellationTicketDetailsModel> parseCancellationTicketDetails(
			String objTicketDetails) {
		// TODO Auto-generated method stub
		ArrayList<CancellationTicketDetailsModel> CancelticketDetailsList = new ArrayList<CancellationTicketDetailsModel>();

		CancellationTicketDetailsModel CancellationticketDetailsModel = new CancellationTicketDetailsModel();
		try {
			JSONObject cancelTicketDetailsObj = new JSONObject(objTicketDetails);

			CancellationticketDetailsModel
					.setcancellable(cancelTicketDetailsObj
							.getBoolean("cancellable"));

			JSONObject cancellationChargesObj = cancelTicketDetailsObj
					.getJSONObject("cancellationCharges");
			if (cancellationChargesObj.optJSONArray("entry") instanceof JSONArray) {
				ArrayList<CancellationTicketDetailsModel> cancellationChargesList = new ArrayList<CancellationTicketDetailsModel>();

				JSONArray entryArray = cancellationChargesObj
						.getJSONArray("entry");

				for (int i = 0; i < entryArray.length(); i++) {
					JSONObject entryArrayObj = entryArray.getJSONObject(i);
					CancellationticketDetailsModel.setKey(entryArrayObj
							.getString("key"));
					CancellationticketDetailsModel.setvalue(entryArrayObj
							.getString("value"));
					cancellationChargesList.add(CancellationticketDetailsModel);
					CancellationticketDetailsModel
							.setCancelTicketList(cancellationChargesList);
				}
			} else if (cancellationChargesObj.optJSONObject("entry") instanceof JSONObject) {
				ArrayList<CancellationTicketDetailsModel> cancellationChargesList = new ArrayList<CancellationTicketDetailsModel>();
				JSONObject entryObj = cancellationChargesObj
						.getJSONObject("entry");
				CancellationticketDetailsModel
						.setKey(entryObj.getString("key"));
				CancellationticketDetailsModel.setvalue(entryObj
						.getString("value"));
				cancellationChargesList.add(CancellationticketDetailsModel);
				CancellationticketDetailsModel
						.setCancelTicketList(cancellationChargesList);
			}
			JSONObject fareObj = cancelTicketDetailsObj.getJSONObject("fares");
			if (fareObj.optJSONArray("entry") instanceof JSONArray) {
				ArrayList<CancellationTicketDetailsModel> fareList = new ArrayList<CancellationTicketDetailsModel>();

				JSONArray fareEntryArray = fareObj.getJSONArray("entry");

				for (int i = 0; i < fareEntryArray.length(); i++) {
					JSONObject fareEntryArrayObj = fareEntryArray
							.getJSONObject(i);
					CancellationticketDetailsModel.setfareKey(fareEntryArrayObj
							.getString("key"));
					CancellationticketDetailsModel
							.setfareValue(fareEntryArrayObj.getString("value"));
					fareList.add(CancellationticketDetailsModel);
					CancellationticketDetailsModel.setfareList(fareList);
				}
			} else if (fareObj.optJSONObject("entry") instanceof JSONObject) {
				ArrayList<CancellationTicketDetailsModel> fareList = new ArrayList<CancellationTicketDetailsModel>();
				JSONObject fareEntryObj = fareObj.getJSONObject("entry");
				CancellationticketDetailsModel.setfareKey(fareEntryObj
						.getString("key"));
				CancellationticketDetailsModel.setfareValue(fareEntryObj
						.getString("value"));
				fareList.add(CancellationticketDetailsModel);
				CancellationticketDetailsModel.setfareList(fareList);
			}
			CancellationticketDetailsModel
					.setfreeCancellationTime(cancelTicketDetailsObj
							.getString("freeCancellationTime"));
			CancellationticketDetailsModel
					.setpartiallyCancellable(cancelTicketDetailsObj
							.getBoolean("partiallyCancellable"));
			CancellationticketDetailsModel.settatkalTime(cancelTicketDetailsObj
					.getString("tatkalTime"));
			System.out.println("cancellation charge:"
					+ CancellationticketDetailsModel.getCancelTicketList()
							.get(0).getvalue());
			System.out.println("refund fare:"
					+ CancellationticketDetailsModel.getfareList().get(0)
							.getfareValue());
			CancelticketDetailsList.add(CancellationticketDetailsModel);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return CancelticketDetailsList;
	}

	public String getstrTicketNo() {
		return strTicketNo;
	}

	public void setstrTicketNo(String strTicketNo) {
		this.strTicketNo = strTicketNo;
	}

	public ArrayList<String> getseatDetails() {
		return seatDetails;
	}

	public void setSeatDetails(ArrayList<String> seatDetails) {
		this.seatDetails = seatDetails;
	}
	public String getstrJSOn() {
		return strJSOn;
	}

	public void setstrJSOn(String strJSOn) {
		this.strJSOn = strJSOn;
	}
	public String convertToJSON() {
		// TODO Auto-generated method stub
		CancellationTicketDetailsModel cancelTicketDetailsModel = new CancellationTicketDetailsModel();
		JSONObject cancelTicketObj = new JSONObject();
		try {
			cancelTicketObj.put("tin", getstrTicketNo());
			JSONArray seatNameArray = new JSONArray();
			for (int i = 0; i < getseatDetails().size(); i++) {
				String[] strSeattocancel = getseatDetails().get(i).split(";");
				String[] strSeattocanceldigit = strSeattocancel[1].trim()
						.split("\\|");
				String[] strSeattocandigit = strSeattocanceldigit[1].trim()
						.split(":");
				seatNameArray.put(strSeattocandigit[1].trim());
			}
			cancelTicketObj.put("seatsToCancel", seatNameArray);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String jsonStr = cancelTicketObj.toString();
		cancelTicketDetailsModel.setstrJSOn(jsonStr);
		System.out.println("jsonString: " + jsonStr);
		return jsonStr;
	}
}
